#pragma once

#include <fstream>
#include <string>
#include "Log.hpp"

namespace Util
{
    class file
    {
    public:
        static bool read(std::string *readbody, const std::string &filename)
        {
            std::fstream ifs(filename, std::fstream::binary | std::fstream::in);
            if (!ifs.is_open())
            {
                LOG(WARNING, "%s: open file error", filename.c_str());
                return false;
            }

            // 获取文件大小
            ifs.seekg(0, ifs.end);
            size_t file_size = ifs.tellg();
            ifs.seekg(0, ifs.beg);

            // 将读取的内容放入readbody中
            readbody->resize(file_size);
            ifs.read(&(*readbody)[0], file_size);
            if (!ifs.good())
            {
                LOG(WARNING, "%s: read file error", filename.c_str());
                ifs.close();
                return false;
            }

            ifs.close();
            return true;
        }
    };
}